from pin_item import Pin


class Cell:
    def __init__(self,name) -> None:
        self.all_ports = {}
        self.type = "default"
        self.all_parameters = {}
        self.used_parameter = {}
        self.attributes = {}
        self.used_ports = set()
        self.connections = {}
        self.name = name
        
        self.attributes["module_not_derived"] = "00000000000000000000000000000001"

        
        pass

    def get_port(self,port_name):
        return self.all_ports[port_name]
        exit(-1)

    def to_dict(self):
        data = dict()
        data["hide_name"] = 0
        data["type"] = self.type
        data["parameters"] = self.used_parameter
        data["attributes"] = self.attributes
        # 计算port_directions
        port_directions = dict()
        for port in self.used_ports:
            port_directions[port.get_name()] = port.get_type()
        data["port_directions"] = port_directions

        # connections
        data["connections"] = dict()
        for name,port in self.connections.items():
            data["connections"][name] = port.get_pins_bits()

        # parameters
        data["parameters"] = dict()
        for k, v in self.used_parameter.items():
            data["parameters"][k] = v

            
        return data


    # target可以是pin或bit类型
    def add_connection(self,target, port_name,pin_index=0):
        bit = target
        if isinstance(target,Pin):
            bit = target.get_bit()
        port = self.get_port(port_name)
        pin = port.get_pin_by_index(pin_index)
        pin.set_bit(bit)
        if port.get_name() not in self.connections.keys():
            self.connections[port.get_name()] = port
        pass
        # 更新used_ports
        self.used_ports.add(port)

    def get_name(self):
        return self.name

        
        


                     



